#include "playgame.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "coin.h"
#include "dataconfig.h"
#include <QDebug>
#include <QPropertyAnimation>
#include <QSound>

PlayGame::PlayGame(QWidget *parent) : QMainWindow(parent)
{

}
PlayGame::PlayGame(int i)
{
    this->levelNum = i;
    QSound * backsound = new QSound(":/Image/BackButtonSound.wav", this);
    /* 设置窗口的大小 */
    this->setFixedSize(320, 588);
    /* 设置窗口标题 */
    QString title = QString("第%1关").arg(i);
    this->setWindowTitle(title);
    /* 设置窗口图标 */
    this->setWindowIcon(QIcon(":/Image/Coin0001.png"));
    /* 获取菜单栏，设置菜单和菜单项 */
    QMenuBar *mbar = menuBar();
    QMenu *menu = new QMenu("开始", this);
    QAction *action = new QAction("退出", this);
    menu->addAction(action);
    mbar->addMenu(menu);
    /* 做退出键功能 */
    connect(action, &QAction::triggered, this, &QMainWindow::close);
    /* 做返回按键 */
    MyPushButton *backButton = new MyPushButton(":/Image/BackButton.png", ":/Image/BackButtonSelected.png");
    backButton->setParent(this);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());
    connect(backButton, &QPushButton::clicked, this, [=](){
        backsound->play();
        QTimer::singleShot(500, this, [=](){

            emit buttonBack();

        });
    });
    /* 做label显示关卡 */
    QLabel *label = new QLabel(this);
    label->setGeometry(30, this->height() - 50, 120, 50);
    //    label->setText(QString::number(this->levelNum));
    /* 设置字体 */
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(18);
    label->setFont(font);
    QString str = QString("Level: %1").arg(i);
    label->setText(str);

    dataConfig config;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            configData[i][j] = config.mData[this->levelNum][i][j];
            qDebug() << configData[i][j];
        }
    }

    /* 胜利图片的显示 */
    QLabel *winLabel = new QLabel(this);
    QPixmap tmp_pix;
    tmp_pix.load(":/Image/LevelCompletedDialogBg.png");
    winLabel->setFixedSize(QSize(tmp_pix.width(), tmp_pix.height()));
    winLabel->setPixmap(tmp_pix);
    winLabel->move((this->width() - tmp_pix.width()) * 0.5, -tmp_pix.height());

    /* 创建金币背景 */
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            QLabel *label_tmp = new QLabel(this);
            label_tmp->setGeometry(0, 0, 50, 50);
            label_tmp->setPixmap(QPixmap(":/Image/BoardNode(1).png"));
            label_tmp->move(57 + i * 50, 200 + j * 50);

            QString string;
            if (configData[i][j]) {
                string = ":/Image/Coin0001.png";
            } else {
                string = ":/Image/Coin0008.png";
            }
            Coin *coin = new Coin(string);
            coin->setParent(this);
            coin->move(59 + i * 50, 204 + j * 50);

            btnArray[i][j] = coin;

            coin->posX = i;
            coin->posY = j;
            coin->flag = configData[i][j];

            connect(coin, &Coin::clicked, this, [=](){
                coin->changeFlag();
                this->configData[coin->posX][coin->posY] = !this->configData[coin->posX][coin->posY];

                QTimer::singleShot(300, this, [=](){
                    if (coin->posX + 1 <= 3) {
                        btnArray[coin->posX + 1][coin->posY]->changeFlag();
                        this->configData[coin->posX + 1][coin->posY] = !this->configData[coin->posX + 1][coin->posY];
                    }
                    if (coin->posX - 1 >= 0) {
                        btnArray[coin->posX - 1][coin->posY]->changeFlag();
                        this->configData[coin->posX - 1][coin->posY] = !this->configData[coin->posX - 1][coin->posY];
                    }

                    if (coin->posY + 1 <= 3) {
                        btnArray[coin->posX][coin->posY + 1]->changeFlag();
                        this->configData[coin->posX][coin->posY + 1] = !this->configData[coin->posX][coin->posY + 1];
                    }

                    if (coin->posY - 1 >= 0) {
                        btnArray[coin->posX][coin->posY - 1]->changeFlag();
                        this->configData[coin->posX][coin->posY - 1] = !this->configData[coin->posX][coin->posY - 1];
                    }

                    isWin = true;
                    for (int i = 0; i < 4; i++) {
                        for (int j = 0; j < 4; j++) {
                            qDebug() << "isWin = " << isWin;
                            isWin &= btnArray[i][j]->flag;
                        }
                    }

                    if (isWin) {
                        QSound * winsound = new QSound(":/Image/LevelWinSound.wav", this);
                        winsound->play();
                        //                        qDebug() << "win win win !!!";
                        for (int i = 0; i < 4; i++) {
                            for (int j = 0; j < 4; j++) {
                                btnArray[i][j]->isWin = true;
                            }
                        }
                        //                        coin->isWin = true;
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel,  "geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });
            });
        }
    }

}
/* 重写绘画函数 */
void PlayGame::paintEvent(QPaintEvent *)
{
    /* 加载图片资源 */
    QPixmap pix;
    pix.load(":/Image/PlayLevelSceneBg.png");
    /* 创建画家 */
    QPainter painter(this);
    /* 画图 */
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
    pix.load(":/Image/Title.png");
    pix = pix.scaled(pix.width() * 0.7, pix.height() * 0.7);
    painter.drawPixmap(0, 0, pix);
}
